﻿
using HomagChina.CCT.Views;
using HomagGroup.Base.UI;
using Proxy;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using static HomagChina.CCT.Utility;

namespace HomagChina.CCT.ViewModels
{
    public class SettingsViewModel : HomagGroup.Base.UI.NotifyPropertyChangedBase
    {
        #region Handle

        private CommonHandle commonHandle;
        private DeleteInfoHandle Handle_Delete;
        private SearchInfoHandle Handle_Search;

        public RelayCommand EditCommand { get; }
        public RelayCommand DeleteCommand { get; }
        public RelayCommand SearchCommand { get; }
        public RelayCommand UnLoadedCommand { get; }
        public RelayCommand LoadedCommand { get; }

        #endregion Handle

        #region 属性

        private string _IP;

        public string IP
        {
            get => _IP;
            set { _IP = value; Query(); OnPropertyChanged(); }
        }

        private string _Type;

        public string Type
        {
            get => _Type;
            set { _Type = value; Query(); OnPropertyChanged(); }
        }

        private string _Describe;

        public string Describe
        {
            get => _Describe;
            set { _Describe = value; Query(); OnPropertyChanged(); }
        }

        private string _MachineName;

        public string MachineName
        {
            get => _MachineName;
            set { _MachineName = value; OnPropertyChanged(); }
        }

        private string _SourcePos;

        public string SourcePos
        {
            get => _SourcePos;
            set { _SourcePos = value; Query(); OnPropertyChanged(); }
        }

        private string _TargetPos;

        public string TargetPos
        {
            get => _TargetPos;
            set { _TargetPos = value; OnPropertyChanged(); }
        }

        private ObservableCollection<string> settingsOptions;

        public ObservableCollection<string> SettingsOptions
        {
            get => settingsOptions;
            set { settingsOptions = value; OnPropertyChanged(); }
        }

        private ObservableCollection<SetttingAppliancesModel> _List_AppliancesInfoMode;

        public ObservableCollection<SetttingAppliancesModel> List_AppliancesInfoMode
        {
            get => _List_AppliancesInfoMode;
            set { _List_AppliancesInfoMode = value; OnPropertyChanged(); }
        }

        private ObservableCollection<SetttingDirectionModel> _List_DirectionInfoMode;

        public ObservableCollection<SetttingDirectionModel> List_DirectionInfoMode
        {
            get => _List_DirectionInfoMode;
            set { _List_DirectionInfoMode = value; OnPropertyChanged(); }
        }

        private ObservableCollection<SetttingBasicModel> _List_ScannerInfoMode;

        public ObservableCollection<SetttingBasicModel> List_ScannerInfoMode
        {
            get => _List_ScannerInfoMode;
            set { _List_ScannerInfoMode = value; OnPropertyChanged(); }
        }

        private ObservableCollection<SetttingBasicModel> _List_PathInfoMode;

        public ObservableCollection<SetttingBasicModel> List_PathInfoMode
        {
            get => _List_PathInfoMode;
            set { _List_PathInfoMode = value; OnPropertyChanged(); }
        }

        private SetttingsOptionsModel _CurrentContent;

        public SetttingsOptionsModel CurrentContent
        {
            get => _CurrentContent;
            set
            {
                _CurrentContent = value;
                OnPropertyChanged();
            }
        }

        /// <summary>
        /// Dropdown list
        /// </summary>
        private object _SelectedValue;

        public object SelectedValue
        {
            get => _SelectedValue;
            set
            {
                _SelectedValue = value;
                OnPropertyChanged();
            }
        }

        private string _SelectedOption = "Appliances";

        public string SelectedOption
        {
            get => _SelectedOption;
            set
            {
                _SelectedOption = value;
                HandleChangeType(value);
                OnPropertyChanged();
            }
        }

        public IDictionary<int, System.Drawing.Brush> CustomStatus { get; set; }

        #endregion 属性

        #region 构造函数

        public SettingsViewModel()
        {
            commonHandle = new CommonHandle();
            Handle_Delete = new DeleteInfoHandle();
            EditCommand = new RelayCommand(HandleEditCommand);
            DeleteCommand = new RelayCommand(HandleDeleteCommand);
            SearchCommand = new RelayCommand(Query);
            LoadedCommand = new RelayCommand(ViewloadedCommand);
            UnLoadedCommand = new RelayCommand(ViewUnloadedCommand);
            Handle_Search = new SearchInfoHandle();
            CustomStatus = new Dictionary<int, System.Drawing.Brush>
            {
                { 0, Application.Current.TryFindResource(HomagGroup.Base.UI.Themes.Resources.InternalKeys.MessageExclamationImage) as System.Drawing.Brush },
                { 1, Application.Current.TryFindResource(HomagGroup.Base.UI.Themes.Resources.InternalKeys.TableErrorStateImage) as System.Drawing.Brush }
            };
            //SettingsOptions = new ObservableCollection<string> { "Appliances", "DirectionConfig", "ScannerConfig", "PathConfig" };//
            SettingsOptions = new ObservableCollection<string> { "Appliances", "DirectionConfig", "PathConfig" };//
            HandleChangeType("Appliances");
        }

        #endregion 构造函数

        #region 函数

        private void ViewloadedCommand()
        {
        }

        /// <summary>
        /// Unloaded
        ///
        /// </summary>
        private void ViewUnloadedCommand()
        {
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="value"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void HandleChangeType(string value)
        {
            CurrentContent = new SetttingsOptionsModel()
            {
                Name = value,
                viewModel = this
            };
            Query();
        }

        /// <summary>
        ///
        /// </summary>
        public void Query()
        {
            if (SelectedOption == "Appliances")
            {
                var Result = Handle_Search.GetSettingsAppliancesInfoByIp(IP);
                if (Result != null)
                    List_AppliancesInfoMode = Result;
            }
            else if (SelectedOption == "DirectionConfig")
            {
                var Result = Handle_Search.GetSettingsDirectionInfoBySourcePos(SourcePos);
                if (Result != null)
                    List_DirectionInfoMode = Result;
            }
            else if (SelectedOption == "ScannerConfig")
            {
                var Result = Handle_Search.GetSettingsBasicInfoByDescribe(string.IsNullOrWhiteSpace(Describe) ? "Scanner" : Describe);
                if (Result != null)
                    List_ScannerInfoMode = Result;
            }
            else if (SelectedOption == "PathConfig")
            {
                var Result = Handle_Search.GetSettingsBasicInfoByDescribe(string.IsNullOrWhiteSpace(Describe) ? "Path" : Describe);
                if (Result != null)
                    List_PathInfoMode = Result;
            }
            else
            {
            }
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="obj"></param>
        public void HandleEditCommand(object SelectItem)
        {
            if (SelectItem != null && SelectedOption == "Appliances")
            {
                SettingsAppliancesDialogViewModel settingsDialogViewModel = new SettingsAppliancesDialogViewModel(SelectItem as SetttingAppliancesModel, true);
                UIAdjustments.CurrentThreadDialogManager.ShowDialog<SettingsAppliancesDialog>(settingsDialogViewModel, null);
            }
            else if (SelectItem != null && SelectedOption == "DirectionConfig")
            {
                SettingsDirectionDialogViewModel settingsDialogViewModel = new SettingsDirectionDialogViewModel(SelectItem as SetttingDirectionModel, true);
                UIAdjustments.CurrentThreadDialogManager.ShowDialog<SettingsDirectionDialog>(settingsDialogViewModel, null);
            }
            else if (SelectItem != null && SelectedOption == "ScannerConfig")
            {
                SettingsScannerDialogViewModel settingsDialogViewModel = new SettingsScannerDialogViewModel(SelectItem as SetttingsModel, true);
                UIAdjustments.CurrentThreadDialogManager.ShowDialog<SettingsScannerDialog>(settingsDialogViewModel, null);
            }
            else if (SelectItem != null && SelectedOption == "PathConfig")
            {
                SettingsPathDialogViewModel settingsDialogViewModel = new SettingsPathDialogViewModel(SelectItem as SetttingsModel, true);
                UIAdjustments.CurrentThreadDialogManager.ShowDialog<SettingsPathDialog>(settingsDialogViewModel, null);
            }
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="SelectItem"></param>
        public void HandleDeleteCommand(object obj)
        {
            if (HomagGroup.Base.UI.Windows.MessageBox.Show(GetLocalText("Are you sure you want to delete this recipe") + "\r\n", GetLocalText("Prompt message"), MessageBoxButton.OKCancel, MessageBoxImage.Question) == MessageBoxResult.OK)
            {
                if (SelectedOption == "Appliances")
                {
                    SetttingAppliancesModel list = (SetttingAppliancesModel)obj;
                    if (Handle_Delete.DeletedSettingInfo(list.Id, SelectedOption))
                    {
                        HomagGroup.Base.UI.Windows.MessageBox.Show(GetLocalText("Deleted successfully"));
                    }
                }
                else if (SelectedOption == "DirectionConfig")
                {
                    SetttingDirectionModel list = (SetttingDirectionModel)obj;
                    if (Handle_Delete.DeletedSettingInfo(list.Id, SelectedOption))
                    {
                        HomagGroup.Base.UI.Windows.MessageBox.Show(GetLocalText("Deleted successfully"));
                    }
                }
                Query();
            }
        }

        /// <summary>
        /// 打开弹窗
        /// </summary>
        public void OpenDialog(object data)
        {
            if (SelectedOption == "Appliances")
            {
                SettingsAppliancesDialogViewModel SettingsDialogViewModel = new SettingsAppliancesDialogViewModel(null, false);
                UIAdjustments.CurrentThreadDialogManager.ShowDialog<SettingsAppliancesDialog>(SettingsDialogViewModel, null);
            }
            else if (SelectedOption == "DirectionConfig")
            {
                SettingsDirectionDialogViewModel SettingsDialogViewModel = new SettingsDirectionDialogViewModel(null, false);
                UIAdjustments.CurrentThreadDialogManager.ShowDialog<SettingsDirectionDialog>(SettingsDialogViewModel, null);
            }
            Query();
        }
    }

    #endregion 函数
}